var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    nail: [0,0,0],
    zdynail:[],
    naillist: ['未联系', '无需继续联系', '继续跟进',],
    currentTab: 0,
    showModal: false,
    recommend :'1',
    tjzssx : '0',
    inputVal : '',
    tmpstr: ''
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  reset: function () {
    this.setData({
      nail: [0, 0, 0],
      zdynail: [],
      tmpstr: ''
    })
  },

  preventTouchMove: function () {

  },
  go: function (e) {
    var that = this;
    var kk = that.data.currentTab
    console.log("curtab=", kk)
    var nail = that.data.nail
    //获取选择状态
    var status = ''
    if (nail[0] == '1') {
      status = '0'
    }
    if (nail[1] == '1') {
      status = '1'
    }
    if (nail[2] == '1') {
      status = '2'
    }
    //获取关键词
    var keyword = e.detail.value
    var tmpstr = that.data.tmpstr
    console.log('keyword=' + keyword)
    var recommend = that.data.recommend
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/contacts/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["u"] = u
    k["isrecommend"] = '0'
    k["recommend"] = recommend
    var sign = ''
    if (status != '') {
      k["status"] = status
      //判断关键词
      if (keyword != '') {
        k["keyword"] = keyword
        if (tmpstr != '') {
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'status' + status + 'tags' + tmpstr + 'timestamp' + timestamp + app.globalData.picotoken
        } else {
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'status' + status + 'timestamp' + timestamp + app.globalData.picotoken

        }
      } else {
        if (tmpstr != '') {
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'status' + status + 'tags' + tmpstr + 'timestamp' + timestamp + app.globalData.picotoken
        } else {
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'status' + status + 'timestamp' + timestamp + app.globalData.picotoken
        }

      }
    } else {
      if (keyword != '') {
        k["keyword"] = keyword
        if (tmpstr != '') {
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'tags' + tmpstr + 'timestamp' + timestamp + app.globalData.picotoken
        } else {
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'timestamp' + timestamp + app.globalData.picotoken
        }

      } else {
        if (tmpstr != '') {
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'tags' + tmpstr + 'timestamp' + timestamp + app.globalData.picotoken
        } else {
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'timestamp' + timestamp + app.globalData.picotoken
        }

      }
    }
    console.log('sign=' + sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    // curb64 = 'ECC8FD59E6E11CFD04E00ADA9ACEA342'
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        console.log('curdata=' + curdata)
        that.setData({
          personMesg: curdata
        });
        var size = curdata.length;
        console.log("size=" + size)
        if (size != 0) {
          var fullname = curdata[0].fullname
          console.log('fullname=' + fullname)
        }

      }, fail: res => {
        console.log(res, "error")
      }

    })


    this.setData({
      showModal: false
    })

  },
  goSearch: function (e) {
    var that = this;
    var kk = that.data.currentTab
    console.log("curtab=", kk)
    var nail = that.data.nail
    //获取选择状态
    var status = ''
    if (nail[0] == '1') {
      status = '0'
    }
    if (nail[1] == '1') {
      status = '1'
    }
    if (nail[2] == '1') {
      status = '2'
    }
    //获取关键词
    var keyword = that.data.inputVal
    var tmpstr = that.data.tmpstr
    console.log('keyword=' + keyword)
    var recommend = that.data.recommend
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/contacts/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["u"] = u
    k["isrecommend"] = '0'
    k["recommend"] = recommend
    var sign = ''
    if (status != '') {
      k["status"] = status
      //判断关键词
      if (keyword != ''){
        k["keyword"] = keyword
        if(tmpstr!=''){
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'status' + status + 'tags' + tmpstr+'timestamp' + timestamp + app.globalData.picotoken
        }else{
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'status' + status + 'timestamp' + timestamp + app.globalData.picotoken

        }
      }else{
        if (tmpstr != '') {
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'status' + status + 'tags' + tmpstr +'timestamp' + timestamp + app.globalData.picotoken
        }else{
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'status' + status + 'timestamp' + timestamp + app.globalData.picotoken
        }

      }
    } else {
      if (keyword != '') {
        k["keyword"] = keyword
        if (tmpstr != ''){
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'tags' + tmpstr +'timestamp' + timestamp + app.globalData.picotoken
        }else{
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0keyword' + keyword + 'recommend' + recommend + 'timestamp' + timestamp + app.globalData.picotoken
        }
        
      }else{
        if (tmpstr != '') {
          k["tags"] = tmpstr
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'tags' + tmpstr+ 'timestamp' + timestamp + app.globalData.picotoken
        }else{
          sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend + 'timestamp' + timestamp + app.globalData.picotoken
        }
        
      }
    }
    console.log('sign='+sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
   // curb64 = 'ECC8FD59E6E11CFD04E00ADA9ACEA342'
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        console.log('curdata=' + curdata)
        that.setData({
          personMesg: curdata
        });
        var size = curdata.length;
        console.log("size=" + size)
        if (size != 0) {
          var fullname = curdata[0].fullname
          console.log('fullname=' + fullname)
        }
      }, fail: res => {
        console.log(res, "error")
      }

    })


    this.setData({
      showModal: false
    })
   
  },
  onShow: function () { 
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数 
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Contacts'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '联系人'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/contacts/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["isrecommend"] = '0'
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            personMesg: curdata
          });
          var size = curdata.length;
          console.log("size=" + size)
          if (size != 0) {
            var attendeeid = curdata[0].attendeeid
            // console.log('attendeeid=' + attendeeid)
            //调用pico标签列表
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '/attendee/tags/list'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["attendeeid"] = attendeeid
            k["u"] = u
            var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            console.log(sign)
            var curb64 = md5.hexMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                that.setData({
                  tagsDetail: curdata
                });
              }, fail: res => {
                console.log(res, "error")
              }

            })


          }

        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },

  onLoad: function (options) {

    var that = this;
    // 页面初始化 options为页面跳转所带来的参数 
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))){
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/contacts/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["isrecommend"] = '0'
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            personMesg: curdata
          });
          var size = curdata.length;
          console.log("size=" + size)
          if (size != 0) {
            var attendeeid = curdata[0].attendeeid
            // console.log('attendeeid=' + attendeeid)
            //调用pico标签列表
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '/attendee/tags/list'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["attendeeid"] = attendeeid
            k["u"] = u
            var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            console.log(sign)
            var curb64 = md5.hexMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                that.setData({
                  tagsDetail: curdata
                });
              }, fail: res => {
                console.log(res, "error")
              }

            })


          }

        }, fail: res => {
          console.log(res, "error")
        }

      })
    }else{
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  /**
  tjzs: function () {
    var that = this;
    var kk = that.data.currentTab
    
    wx.showToast({
      title: kk,
      icon: 'success',
      duration: 2000
    })
  },
    */
  //点击切换
  zx: function (e) {
    var that = this;
    that.clickTab(e)
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/contacts/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["u"] = u
    k["isrecommend"] = '0'
    var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        that.setData({
          personMesg: curdata
        });
        var size = curdata.length;
        console.log("size=" + size)
        if (size != 0) {
          var fullname = curdata[0].fullname
          console.log('fullname=' + fullname)
        }

      }, fail: res => {
        console.log(res, "error")
      }

    })
  },
  tjzs: function (e) {
    var that = this;
    that.clickTab(e)
    //默认推荐指数倒叙排列
    var recommend = that.data.recommend
    if (recommend == '0') {
      recommend = '1'
    } else {
      recommend = '0'
    }
    //第一次推荐指数倒叙排列
    var tjzssx = that.data.tjzssx
    console.log('tjzssx', tjzssx)
    if (tjzssx=='0'){
      recommend='1'
      that.setData({
        tjzssx: '1'
      });
    }
    that.setData({
      recommend: recommend
    });
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/contacts/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["u"] = u
    k["isrecommend"] = '0'
    k["recommend"] = recommend
    var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0recommend' + recommend+'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        that.setData({
          personMesg: curdata
        });
        var size = curdata.length;
        console.log("size=" + size)
        if (size != 0) {
          var fullname = curdata[0].fullname
          console.log('fullname=' + fullname)
        }

      }, fail: res => {
        console.log(res, "error")
      }

    })
  },
  nailchoice(e) {
    var that = this;
    that.choice(that, 'nail', e)
    var nail = that.data.nail
    var tmpstr = ''
    console.log(nail)

    for (var i in nail) {
      if (nail[i] == 1) {
        tmpstr += that.data.naillist[i] + ","
      }
    }
    console.log('tmpstr=' + tmpstr)
  },
  choice(that, key, e) {
      
    var nail = that.data[key]
    
    console.log('nail=' + nail)
    var it = e.currentTarget.dataset.it
    //其他设置为0
    if(nail[it]==1){
      //设置其他为0 
      that.setData({
        nail: [0, 0, 0]
      })
    }else{
      that.setData({
        nail: [0, 0, 0]
      })
      var data = key + '[' + it + ']'
      console.log('data=' + data)
      console.log('nail[it]=' + nail[it])
      console.log('[data]=' + [data])
      if (!nail[it] || nail[it] == 0) {
        that.setData({
          [data]: 1
        })
      } else if (nail[it] == 1) {
        that.setData({
          [data]: 0
        })
      }
    }
   
  },
  zdynailchoice(e) {
    var that = this;
    that.zdychoice(that, 'zdynail', e)
    var zdynail = that.data.zdynail
    var tmpstr = ''

    for (var i in zdynail) {
      if (zdynail[i] == 1) {
        tmpstr += i + "|"
      }
    }
    console.log('tmpstr=' + tmpstr)
    that.setData({
      'tmpstr': tmpstr
    })
  },
  zdychoice(that, key, e) {
    //设置其他为0 
    var zdynail = that.data[key]
    console.log('zdynail=' + zdynail)
    var it = e.currentTarget.dataset.tagid
    //其他设置为0
    var data = key + '[' + it + ']'
    console.log('data=' + data)
    console.log('zdynail[it]=' + zdynail[it])
    console.log('[data]=' + [data])
    if (!zdynail[it] || zdynail[it] == 0) {
      that.setData({
        [data]: 1
      })
    } else if (zdynail[it] == 1) {
      that.setData({
        [data]: 0
      })
    }
  },
  toDetail(e) {
    console.log(e)
    var attendeeid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../pageDetail/pageDetails?attendeeid=' + attendeeid,
    })
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  }

})